package com.example.book.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.book.entity.Type;
import com.example.book.mapper.TypeMapper;
import com.example.book.service.ITypeService;
import com.example.common.dtos.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 凡哥哥
 * @since 2023-10-16
 */
@RestController
@RequestMapping("/type")
@Slf4j
@Api(tags = "图书标签控制器")
public class TypeController {
    @Autowired
    private ITypeService typeService;

    @Autowired
    private TypeMapper typeMapper;

    /**
     * 查询图书类型信息
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @ApiOperation(value = "查询图书类型信息")
    @GetMapping("/page")
    public ResponseResult<Page> page(Integer page, Integer pageSize, String name) {

        return typeService.pageInfo(page, pageSize, name);
    }

    /**
     * 查询全部图书类型
     * @param
     * @param
     * @param
     * @return
     */
    @ApiOperation(value = "查询全部图书类型")
    @GetMapping("/all")
    public ResponseResult<Type> page() {

        return ResponseResult.okResult(typeMapper.typeAll());
    }

    /**
     * 添加图书类型
     * @param type
     * @return
     */
    @ApiOperation(value = "添加图书类型")
    @PostMapping
    public ResponseResult add(@RequestBody Type type){
        return typeService.addType(type);
    }

    /**
     * 数据回显
     * @param id
     * @return
     */
    @ApiOperation(value = "数据回显")
    @GetMapping("/{id}")
    public ResponseResult type(@PathVariable Long id){
        return typeService.type(id);
    }

    /**
     * 修改方法
     * @param type
     * @return
     */
    @ApiOperation(value = "修改方法")
    @PutMapping
    public ResponseResult<String> update(@RequestBody Type type){
        log.info("修改图书标签信息:{}",type);
        typeService.updateById(type);
        return ResponseResult.okResult("修改图书标签成功");
    }

    /**
     * 修改状态
     *
     * @param
     * @return
     */
    @ApiOperation(value = "修改状态")
    @PutMapping("/status")
    public ResponseResult status(Long id) {
        return ResponseResult.okResult(typeService.updateStatus(id));
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @ApiOperation(value = "删除")
    @DeleteMapping
    public ResponseResult delete(Long [] ids) {
        return typeService.deleteBooks(ids);
    }
}

